package top.xiesen.collection;

import java.util.HashMap;

/**
 * 自定义实现Map的功能
 * 简单版
 * @author Allen
 *	map:存放键值对,根据键对象找值对象,键不能重复
 */
public class MyMap001 {

	MyEntry[] arr = new MyEntry[999];
	int size;
	
	public void put(Object key, Object value){
		MyEntry e = new MyEntry(key,value);
		// 如果数组中包含key,遍历数组,覆盖value的值 
		for (int i = 0; i < size; i++) {
			// 解决键值对重复问题
			if(arr[i].key.equals(key)){
				arr[i].value = value;
				return ;
			}
		}
		arr[size++] = e;
	}
	
	public Object get(Object key){
		for (int i = 0; i < size; i++) {
			if(arr[i].key.equals(key)){
				return arr[i].value;
			}
		}
		return null;
	}
	// 遍历是否包含key值
	public boolean containsKey(Object key){
		for (int i = 0; i < size; i++) {
			if(arr[i].key.equals(key)){
				return true;
			}
		}
		return false;
	}
	// 遍历是否包含value值
	public boolean containsValue(Object value){
		for (int i = 0; i < size; i++) {
			if(arr[i].value.equals(value)){
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args) {
		MyMap001 m = new MyMap001();
		m.put(1, 11111);
		m.put(2 ,222222222);
		System.out.println(m.get(1));
	}
	
}

class MyEntry{
	Object key;
	Object value;
	
	public MyEntry(Object key, Object value) {
		super();
		this.key = key;
		this.value = value;
	}
	
}